package com.msb.common.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;

public class ListValueConstrainValidator implements ConstraintValidator<ListValue, Integer> {

    private HashSet<Integer> set = new HashSet<>();


    /**
     * 初始话方法
     * 举例：@value(val={0,1})
     * 获取到1，0
     * @param constraintAnnotation annotation instance for a given constraint declaration
     */
    @Override
    public void initialize(ListValue constraintAnnotation) {
        //0,1
        int[] val = constraintAnnotation.val();
        for (int i :val) {
            set.add(i);
        }

    }

    /**
     * 判断校验是否成功的方法
     * @param value 客户端传递的值，判断value是否是0，1
     * @param context context in which the constraint is evaluated
     *
     * @return
     */
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return set.contains(value);
    }
}
